const express = require('express');
const router = express.Router();
const { asyncHandle } = require('../getSendResult');
const {
    findBackstageFolder,
    findBackstageFolderAll,
    changeBackstageFolderIsDelete,
    findBackstageFile,
    findBackstageFileAll,
    changeBackstageFileIsDelete,
    deleteBackstageFile,
    deleteBackstageFileArr,
    deleteBackstageFolder,
    deleteBackstageFolderArr,
    deleteFileFolder,//删除文件夹同时删除文件
    deleteFileFolderArr,//删除多个文件夹时同时删除文件
} = require('../../services/backstage-folder');

// 查询文件夹,可分页,可模糊查询
router.get('/findBackstageFolder', asyncHandle(
    async (req, res, next) => {
        const page = req.query.page;
        const limit = req.query.limit;
        const search = req.query.search;
        const userId = req.query.userId;
        return {
            data: await findBackstageFolder(page, limit, search, userId),
            total: (await findBackstageFolderAll(search, userId)).length
        }

    }
))

// 改变文件夹是否可删除的权限
router.post('/changeBackstageFolderIsDelete', asyncHandle(
    async (req, res, next) => {
        const isDelete = req.body.isDelete;
        const userId = req.body.userId;
        const id = req.body.id;
        return await changeBackstageFolderIsDelete(isDelete, userId, id);
    }
))

// 通过文件夹id查询文件,可分页，可模糊查询
router.post('/findBackstageFile', asyncHandle(
    async (req, res, next) => {
        const page = req.body.page;
        const limit = req.body.limit;
        const search = req.body.search;
        const folderId = req.body.folderId;
        return {
            data: await findBackstageFile(page, limit, search, folderId),
            total: (await findBackstageFileAll(search, folderId)).length
        }
    }
))

// 改变文件是否可删除的权限
router.post('/changeBackstageFileIsDelete', asyncHandle(
    async (req, res, next) => {
        const isDelete = req.body.isDelete;
        const fileId = req.body.fileId;
        const folderId = req.body.folderId;
        return await changeBackstageFileIsDelete(isDelete, fileId, folderId);
    }
))

// 删除单个文件
router.get('/deleteBackstageFile', asyncHandle(
    async (req, res, next) => {
        const fileId = req.query.fileId;
        const folderId = req.query.folderId;
        return await deleteBackstageFile(fileId, folderId);
    }
));

// 删除多个文件
router.post('/deleteBackstageFileArr', asyncHandle(
    async (req, res, next) => {
        const fileIdArr = req.body.fileIdArr;
        const folderId = req.body.folderId;
        return await deleteBackstageFileArr(fileIdArr, folderId);
    }
))

// 删除单个文件夹,删除单个文件的同时需要删除文件夹下的文件
router.post('/deleteBackstageFolder', asyncHandle(
    async (req, res, next) => {
        const folderId = req.body.folderId;
        const userId = req.body.userId;
        await deleteFileFolder(folderId);
        return await deleteBackstageFolder(folderId, userId);
    }
))

// 删除多个文件夹,删除多个个文件的同时需要删除文件夹下的文件
router.post('/deleteBackstageFolderArr', asyncHandle(
    async (req, res, next) => {
        const folderIdArr = req.body.folderIdArr;
        const userId = req.body.userId;
        await deleteFileFolderArr(folderIdArr);
        return await deleteBackstageFolderArr(folderIdArr, userId);
    }
))
module.exports = router;